class_name InputActionChecker
## Input Action Checker
## Version: 0.1.0

var action_name: String = ""

var is_double_click_started := false

var is_pressed := false

var is_just_pressed := false

var is_long_pressed := false

var is_released := false

var _elapsed_time := 0.0

var _last_press_start_time := 0.0

var _double_click_threshold := 0.3

var _long_pressed_threshold := 1.0


func _ready() -> void:
    assert(action_name != "", "Action name must be set!")


func on_update(delta: float) -> void:
    # 累计经过的游戏时间（当游戏暂停时delta会自动变为0）
    _elapsed_time += delta

    is_just_pressed = Input.is_action_just_pressed(action_name)

    if Input.is_action_just_pressed(action_name):
        # 使用累计时间判断双击
        if _elapsed_time - _last_press_start_time < _double_click_threshold:
            is_double_click_started = true
        # 记录最后一次按下开始时间
        _last_press_start_time = _elapsed_time
    
    if Input.is_action_pressed(action_name):
        is_pressed = true
        # 使用累计时间判断长按
        if _elapsed_time - _last_press_start_time > _long_pressed_threshold:
            is_long_pressed = true

    if Input.is_action_just_released(action_name):
        is_released = true
        is_pressed = false
        is_long_pressed = false
        is_double_click_started = false
    else:
        is_released = false
